package com.cardone.platform.usercenter.controller;

import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cardone.context.Contexts;
import com.cardone.common.dto.PaginationDto;
import com.cardone.common.dto.ReturnDataDto;
import com.cardone.common.util.ReturnDataUtils;
import com.cardone.context.ContextHolder;
import com.cardone.platform.usercenter.dto.UserDto;
import com.cardone.platform.usercenter.service.UserService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * 用户
 *
 * @author yaohaitao
 */
@Deprecated
@Setter
@Slf4j
@RequestMapping("/usercenter/user/")
@Controller
public class UserController {

	/**
	 * 用户：删除
	 * 
	 * @param deleteUser
	 *            用户对象
	 * @return 处理结果，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "delete.json" }, method = RequestMethod.POST)
	@ResponseBody
  public Callable<ReturnDataDto<String>> deleteJson(final UserDto deleteUser) {
		return new Callable<ReturnDataDto<String>>() {
			public ReturnDataDto<String> call() throws Exception {
				try {
					ContextHolder.getBean(UserService.class).deleteByIds(deleteUser);
					
					return ReturnDataUtils.newReturnData();
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, log);
				}
			}
		};
	}

	/**
	 * 用户：编辑
	 * 
	 * @param findUser
	 *            用户对象
	 * @return 实体对象集，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "findById.json" })
	@ResponseBody
	public Callable<ReturnDataDto<UserDto>> findByIdJson(final UserDto findUser, final Model model) {
		return new Callable<ReturnDataDto<UserDto>>() {
			@Override
			public ReturnDataDto<UserDto> call() throws Exception {
				try {
					final UserDto user = ContextHolder.getBean(UserService.class).findById(UserDto.class, findUser);

					return ReturnDataUtils.newReturnData(user);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, UserController.log);
				}
			}
		};
	}

	/**
	 * 用户：分页查询
	 * 
	 * @param paginationUser
	 *            用户对象
	 * @return 分页，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "paginationByLikeCode.json" })
	@ResponseBody
  public Callable<ReturnDataDto<PaginationDto<UserDto>>> paginationByLikeCodeJson(final UserDto paginationUser) {
		return new Callable<ReturnDataDto<PaginationDto<UserDto>>>() {
			public ReturnDataDto<PaginationDto<UserDto>> call() throws Exception {
				try {
					final PaginationDto<UserDto> userPagination = ContextHolder.getBean(UserService.class).paginationByLikeCode(UserDto.class, paginationUser);
				
					return ReturnDataUtils.newReturnData(userPagination);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, log);
				}
			}
		};
	}

	/**
	 * 用户：分页查询
	 * 
	 * @param paginationUser
	 *            用户对象
	 * @param model
	 *            上下文映射
	 */
	@Deprecated
	@RequestMapping(value = { "pagination.html" })
  public void paginationHtml(final UserDto paginationUser, final Model model) {
		final PaginationDto<UserDto> userPagination = ContextHolder.getBean(UserService.class).paginationByLikeCode(UserDto.class, paginationUser);
		
		model.addAttribute("userPagination", userPagination);
	}
	
	/**
	 * 用户：分页查询
	 * 
	 * @param findUser
	 *            用户对象
	 * @param model
	 *            上下文映射
	 */
	@Deprecated
	@RequestMapping(value = { "edit.html" })
  public void editHtml(final UserDto findUser, final Model model) {
		final UserDto user = ContextHolder.getBean(UserService.class).findById(UserDto.class, findUser);
		
		model.addAttribute("user", user);
	}

	/**
	 * 用户：查询
	 * 
	 * @param findListUser
	 *            用户对象
	 * @return 用户对象集合，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "findListByLikeCode.json" })
	@ResponseBody
  	public Callable<ReturnDataDto<List<UserDto>>> findListByLikeCodeJson(final UserDto findListUser) {
		return new Callable<ReturnDataDto<List<UserDto>>>() {
			public ReturnDataDto<List<UserDto>> call() throws Exception {
				try {
					final List<UserDto> userList = ContextHolder.getBean(UserService.class).findListByLikeCode(UserDto.class, findListUser);
				
					return ReturnDataUtils.newReturnData(userList);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, log);
				}
			}
		};
	}

	/**
	 * 用户：保存
	 * 
	 * @param saveUser
	 *            用户对象
	 * @return 处理结果，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "save.json" })
	@ResponseBody
  public Callable<ReturnDataDto<String>> saveJson(final UserDto saveUser) {
		return new Callable<ReturnDataDto<String>>() {
			public ReturnDataDto<String> call() throws Exception {
				try {
					ContextHolder.getBean(UserService.class).saveByIdOrCode(UserDto.class, saveUser);
					
					return ReturnDataUtils.newReturnData();
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, log);
				}
			}
		};
	}
}
